<?php
/**
 * Desc:
 * Auth: Shaoming Lu<lushao1012@163.com>
 * Date: 2018/5/29
 * Time: 14:44
 */
namespace App\Http\Controllers;

use App\Http\Services\AjaxService;
use App\Http\Services\MCore;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use App\Http\Services\DB;
use Session;

class ConfigController extends Controller
{
    public function connectMysql(Request $request)
    {
        $host = MCore::get_not_empty_var('host', $request, '请输入服务器地址');
        $username = MCore::get_not_empty_var('username', $request, '请输入用户名');
        $password = MCore::get_not_empty_var('password', $request, '请输入密码');
        $port = MCore::get_var('port', $request, 0);
        $port = $port? $port: 3306;

        $ip = gethostbyname($host);
        // 生成环境不能操作本地数据库
        if (MCore::is_issue_env() == true) {
            if ($ip == '127.0.0.1' || $ip == '47.98.150.56') {
                AjaxService::ajax_return(['error' => '为了安全性考虑，不能使用127.0.0.1和系统服务器IP']);
            }
        }


        $data = [
            'host' => $host,
            'username' => $username,
            'password' => $password,
            'port' =>$port,
            'db_version' => '123',
        ];
        session(['db' => $data]);
        Session::save();

        $pdo = new DB();
        $db_version = $pdo->query('SELECT version() as version');
//        if ($db_version !== false) {
        $data['db_version'] = $db_version[0]['version'];
        $data['token'] = md5(time() . $host . mt_rand(100, 999));
        session(['db' => $data]);
        Session::save();
//        }

        AjaxService::ajax_return(['msg' => 'SUCCESS'], ['token' => $data['token']]);
    }

    public function disconnect()
    {
        session_start();
        session(['db' => null]);
        return 	Redirect::to('/');
    }

    /**
     * 测试连接
     * @access  public
     * @author  Lu Shaoming<lushao1012@163.com>
     * @method  POST
     * @url
     * @param Request $request
     */
    public function testConnect(Request $request)
    {
        $host = MCore::get_not_empty_var('host', $request, '请输入服务器地址');
        $username = MCore::get_not_empty_var('username', $request, '请输入用户名');
        $password = MCore::get_not_empty_var('password', $request, '请输入密码');
        $port = MCore::get_var('port', $request, 0);
        $port = $port? $port: 3306;

        $ip = gethostbyname($host);
        // 生成环境不能操作本地数据库
        if (MCore::is_issue_env() == true) {
            if ($ip == '127.0.0.1' || $ip == '47.98.150.56') {
                AjaxService::ajax_return(['error' => '为了安全性考虑，不能使用127.0.0.1和系统服务器IP']);
            }
        }

        try{
            $this->conn = new \PDO('mysql:host='.$host, $username, $password, [
                \PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES'utf8';"
            ]);

        } catch (\PDOException $e) {
           AjaxService::ajax_return(['error' => 'FAIL']);
            exit;
        }

        AjaxService::ajax_return(['msg' => 'SUCCESS']);
    }
}